package sortedSquares.java;

import java.util.Arrays;

public class sortedSquaresSolution {
    public static void main(String[] args) {
        int[] nums = new int[]{-4,-1,0,3,10};
        int[] ints = sortedSquares(nums);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] sortedSquares(int[] nums) {
        int rigth = nums.length - 1;
        int left = 0;
        int[] result = new int[nums.length];
        int index = nums.length - 1;
        while (left <= rigth) {
            int start = nums[left] * nums[left];
            int end = nums[rigth] * nums[rigth];
            if (start > end) {
                result[index] = start;
                index--;
                left++;
            } else {
                result[index] = end;
                index--;
                rigth--;
            }
        }
        return result;
    }

}
